<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;

//分类表
class Category extends BaseController
{
    
    public function initialize()
    {
        parent::initialize();
    }
    protected $table='Category';
    protected $title='分类管理';
    public function index()
    {
        $p=input();
        $where=[['parent_id','=',0],['country_id','=',cache('lang_id')]];
        $category_id=0;
        if(!empty($p['id']))
        {
            $category_id=intval($p['id']);
            $where=[['parent_id','=',$category_id]];
        }
        if(!empty($p['keyword']))
        {
            $where[]=['name','like','%'.$p['keyword'].'%'];
        }
        $category =Db::name($this->table)->where($where)->order('sort desc,id desc')->select()->toArray();
        $spl='';
        foreach($category as &$v)
        {
            $v['data'] = Db::name('category')->where(['parent_id'=>$v['id']])->select()->toArray();
            $spl = '├　';
            $v['spl'] = $spl;
            if(!empty($v['data']))
            {                    
                foreach($v['data'] as &$vv)
                {      
                    $vv['spl'] = '├　├　';                  
                    $vv['data'] = Db::name('category')->where(['parent_id'=>$vv['id']])->select()->toArray();                   
                    if(!empty($vv['data']))
                    {
                        foreach($vv['data'] as &$vvv)
                        {
                            $vvv['spl'] = '├　├　├　';
                            
                        }
                    }
                }
            }
        }
        View::assign('list',$category);
        View::assign('parent_id',$category_id);
        View::assign('title',$this->title);
        return View::fetch();
    }
    public function modify()
    {
        $p=input();
        $category=array();
        $parent_id=isset($p['parent_id'])?intval($p['parent_id']):1;
        $pid=isset($p['pid'])?intval($p['pid']):0;
       
        if(isset($p['id']))
        {
            $data = Db::name($this->table)->where(['id'=>intval($p['id'])])->find();
            if($data){
               $pid=$data['parent_id'];
            }
            View::assign('data',$data);
        }
        $category = \app\service\CategoryService::getTreeList(['parent_id'=>$parent_id]);
       
        View::assign('category',$category);
        View::assign('parent_id',$parent_id);
        View::assign('pid',$pid);
        return View::fetch();
    }
    public function save()
    {
        $p=input();
        $category=array();
        $parent_id=isset($p['parent_id'])?intval($p['parent_id']):0;
        
        unset($p['file']);
            $p['country_id']=cache('lang_id');
            $p['sign']=empty($p['param'])?'':str_replace("tag=",'',$p['param']);
            if(empty($p['id']))
            {
                $level=0;
                if($p['parent_id']>0)
                {
                    $parent_level = Db::name($this->table)->where(['id'=>intval($p['parent_id'])])->value('level',0);
                    $level = $parent_level+1;
                }
                $p['level']=$level;
                Db::startTrans();
                $res = Db::name($this->table)->insertGetId($p);
                $p['id']=$res;
                Db::commit();
                $id=$res;
            }
            else
            {
                $p['status'] = empty($p['status'])?0:1;
                if(intval($p['parent_id'])==$p['id'])
                {
                    jsonReturn(0,'父级ID不能是自己');
                }
                $parent_catgory=Db::name($this->table)->where(['id'=>intval($p['parent_id'])])->find();
                $p['level']=$parent_catgory['level']+1;
                $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update($p);
                $id=$p['id'];
                if(!$res)
                {
                    jsonReturn(0,'保存失败');
                }
            }
            jsonReturn(1,'保存成功',['id'=>$id]);
    }
    public function setsort()
    {
        $p = input();
        $res = Db::name('category')->where(['id'=>intval($p['id'])])->update(['sort'=>intval($p['sort'])]);
        return jsonRes($res);
    }
   
    public function status()
    {
        $p = input();
        $res = Db::name('category')->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }
    public function remove()
    {
        $p = input();
        $id=explode(',',$p['id']);
        $res = Db::name('category')->where('id','in',$id)->delete();
        if($res)
        {
            $sign=$this->makesign($p);
        }
        return jsonRes($res);
    }
}